import { GUI } from 'three/examples/jsm/libs/dat.gui.module.js';
import { scene , camera , controls  } from './scene.js';
import { TweenMaxTo } from "../libs/common.js";
import { config } from './config.js';
import { iconGroup } from './icon.js';
import { sceneGroup } from "./product.js"; 
import  emitter from "../../mitt/eventbus.js";

import $ from 'jquery';

   
 
    function deviceFileChange(iconName,value)
    {
        iconGroup.children.forEach(element => {
            if(element.name.indexOf(iconName) >= 0)
            {
                element.visible = value;
            }
        });
    }


  

    function cameraTostack(statckName)
    {
       
        if(config.cameraIsRun) return;
        config.cameraIsRun = true;

        var targetPos = {};
        var cameraPos = {};
        if(statckName != "reset")
        {
            var stacks = sceneGroup.getObjectByName(statckName);
            config.cameraTargetStack  = [];
            stacks.traverse(function(child){
                if(child.isInstancedMesh){
                    config.cameraTargetStack.push(child);
                }
            })
            
            var statck = scene.getObjectByName(statckName);
            targetPos = statck.position;
            cameraPos = {
                x:targetPos.x + config.cameraOffset.x,
                y:targetPos.y + config.cameraOffset.y,
                z:targetPos.z + config.cameraOffset.z,
            }

        }else{
            config.cameraTargetStack  = [];

            targetPos = {x:0,y:0,z:0};
            cameraPos = {
                x:config.cameraInitialPos.x,
                y:config.cameraInitialPos.y,
                z:config.cameraInitialPos.z,
            };
        }
    

        var data1 = {
            t_x : controls.target.x,
            t_y : controls.target.y,
            t_z : controls.target.z,
            c_x : camera.position.x,
            c_y : camera.position.y,
            c_z : camera.position.z,
        }

        var data2 = {
            t_x : targetPos.x,
            t_y : targetPos.y,
            t_z : targetPos.z,
            c_x : cameraPos.x,
            c_y : cameraPos.y,
            c_z : cameraPos.z,
        }

        function update()
        {
            camera.position.set(data1.c_x,data1.c_y,data1.c_z);
            controls.target.set(data1.t_x,data1.t_y,data1.t_z);
        }

        function calback()
        {
            config.cameraIsRun = false;
            // console.log(config.cameraTargetStack );
        }

        TweenMaxTo(data1,data2,1,update,calback);
    }


    export { deviceFileChange , cameraTostack};